/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iti.jets.gpts.service;

import com.iti.jets.gpts.home.CommentsHome;
import com.iti.jets.gpts.home.FeedbackHome;
import com.iti.jets.gpts.home.UserHome;
import com.iti.jets.gpts.pojo.Comments;
import com.iti.jets.gpts.pojo.Feedback;
import com.iti.jets.gpts.pojo.User;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import util.HibernateUtil;



/**
 *
 * @author YASMIN-ITI
 */

@Path("/user")
public class UserService {

    
    @Path("/getalluser")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public JSONArray getAllUser()
    {
        System.out.println("Hello Service");
        
        UserHome userHome = new UserHome(HibernateUtil.getSessionFactory());
        List<User> allUser = userHome.getAllUser();
        JSONArray jSONArray = new JSONArray();
        for(User user : allUser)
        {
          
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("id", user.getId());
                jSONObject.put("email", user.getEmail());
             
                jSONArray.put(jSONObject);
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }
        return jSONArray;
        
    }
    
    @Path("/getallcomment")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public JSONArray getAllComments()
    {
           System.out.println("Hello get All Comments");
        
        CommentsHome commentsHome = new CommentsHome(HibernateUtil.getSessionFactory());
        List<Comments> allComments = commentsHome.loadAll();
        JSONArray jSONArray = new JSONArray();
        for(Comments comments : allComments)
        {
          
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("id", comments.getId());
                //jSONObject.put("user",comments.getUser());
                jSONObject.put("comment", comments.getComment());
                jSONObject.put("userID", comments.getUser().getId());
                jSONObject.put("userEmail", comments.getUser().getEmail());
                jSONObject.put("transerviceID", comments.getTranService().getId());
                jSONObject.put("transerviceName", comments.getTranService().getName());
                jSONArray.put(jSONObject);
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }
        return jSONArray;
    }
    
    @Path("/getallfeedbacks")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public JSONArray getAllFeedbacks()
    {
           System.out.println("Hello get All Feedbacks");
        
        FeedbackHome feedbackHome = new FeedbackHome(HibernateUtil.getSessionFactory());
        List<Feedback> allFeedbacks = feedbackHome.loadAll();
        JSONArray jSONArray = new JSONArray();
        for(Feedback feedback : allFeedbacks)
        {
          
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("id", feedback.getId());
                jSONObject.put("rating", feedback.getRating());
                jSONObject.put("userID", feedback.getUser().getId());
                jSONObject.put("userEmail", feedback.getUser().getEmail());
                jSONObject.put("transerviceID", feedback.getTranService().getId());
                jSONObject.put("transerviceName", feedback.getTranService().getName());
                
                jSONArray.put(jSONObject);
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }
        return jSONArray;
    }
    
     @GET
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/removeuser/{id}")
    public String removeUser(@PathParam(value = "id") int id)
    {
//         AttributeType attributeType = new AttributeType();
//        attributeType = dao.getObject(id);
//        dao.delete(attributeType);
        UserHome dao = new UserHome(HibernateUtil.getSessionFactory());
         User userObject = dao.read(id);
        dao.delete(userObject);
        return "done";
    }
    
}